package com.xdb.sqlparser.visitor.functions;

import static com.xdb.sqlparser.visitor.SQLEvalVisitor.EVAL_VALUE;

import java.math.BigDecimal;

import com.xdb.sqlparser.ast.SQLExpr;
import com.xdb.sqlparser.ast.expr.SQLMethodInvokeExpr;
import com.xdb.sqlparser.visitor.SQLEvalVisitor;

public class Char implements Function {

	public final static Char instance = new Char();

	public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
		if (x.getParameters().size() == 0) {
			return SQLEvalVisitor.EVAL_ERROR;
		}

		StringBuffer buf = new StringBuffer(x.getParameters().size());
		for (SQLExpr param : x.getParameters()) {
			param.accept(visitor);

			Object paramValue = param.getAttributes().get(EVAL_VALUE);

			if (paramValue instanceof Number) {
				int charCode = ((Number) paramValue).intValue();
				buf.append((char) charCode);
			} else if (paramValue instanceof String) {
				try {
					int charCode = new BigDecimal((String) paramValue).intValue();
					buf.append((char) charCode);
				} catch (NumberFormatException e) {
				}
			} else {
				return SQLEvalVisitor.EVAL_ERROR;
			}
		}

		return buf.toString();
	}
}
